package cn.iniche.view;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;

import ru.playsoftware.j2meloader.R;

import static ru.playsoftware.j2meloader.util.Constants.PREF_THEME;


/**
 * Created by pwcong on 2016/8/2.
 */
public class SimpleSideBar extends View {

    private OnLetterTouchedChangeListener onLetterTouchedChangeListener;

    public void setOnLetterTouchedChangeListener(OnLetterTouchedChangeListener onLetterTouchedChangeListener) {
        this.onLetterTouchedChangeListener = onLetterTouchedChangeListener;
    }

    // 索引字母数组
    private final String[] mAlphabets = {
            "#", "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J", "K",
            "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W",
            "X", "Y", "Z"
    };

    private final Paint mPaint = new Paint();

    private final int mTextColor;
    private final int mTouchedTextColor;
    private final int mTextSize;
    private String mCurrentChosenAlphabet = null;
    private boolean isTouched = false;


    public SimpleSideBar(Context context) {
        this(context, null, 0);
    }

    public SimpleSideBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SimpleSideBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        String theme = preferences.getString(PREF_THEME, "light");
        mTextSize = (int) (context.getResources().getDisplayMetrics().density * 10 + 0.5F);
        if (theme.equals("dark")) {
            mTextColor = ContextCompat.getColor(context, R.color.dark_color_text_primary);
            mTouchedTextColor = ContextCompat.getColor(context, R.color.dark_color_accent);
        } else {
            mTextColor = ContextCompat.getColor(context, R.color.light_color_text_primary);
            mTouchedTextColor = ContextCompat.getColor(context, R.color.light_color_accent);
        }
    }

    public void setCurrentChosenAlphabet(@Nullable String alphabet) {
        if (alphabet == null) {
            if (mCurrentChosenAlphabet != null) {
                mCurrentChosenAlphabet = null;
                postInvalidate();
            }
            return;
        }

        String value = alphabet.toUpperCase();
        if (!value.equals(mCurrentChosenAlphabet)) {
            mCurrentChosenAlphabet = value;
            postInvalidate();
        }
    }

    public boolean isTouched() {
        return isTouched;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int viewHeight = getHeight();
        int viewWidth = getWidth();
        int heightPerAlphabet = viewHeight / mAlphabets.length;

        for (int i = 0; i < mAlphabets.length; i++) {
            mPaint.setColor(mTextColor);
            mPaint.setTypeface(Typeface.DEFAULT_BOLD);
            mPaint.setTextSize(mTextSize);
            mPaint.setAntiAlias(true);
            if (mAlphabets[i].equals(mCurrentChosenAlphabet)) {
                mPaint.setColor(mTouchedTextColor);
                mPaint.setTextSize(mTextSize + 10);
                mPaint.setFakeBoldText(true);
            }
            float xPos = viewWidth / 2 - mPaint.measureText(String.valueOf(mAlphabets[i])) / 2;
            float yPos = heightPerAlphabet * i + heightPerAlphabet;
            canvas.drawText(String.valueOf(mAlphabets[i]), xPos, yPos, mPaint);
            mPaint.reset();

        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        float touchYPos = event.getY();
        int currentTouchIndex = (int) (touchYPos / getHeight() * mAlphabets.length);
        String currentTouchAlphabet = null;

        if (currentTouchIndex >= 0 && currentTouchIndex < mAlphabets.length) {
            currentTouchAlphabet = mAlphabets[currentTouchIndex];
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isTouched = true;
            case MotionEvent.ACTION_MOVE:
                if (currentTouchAlphabet != null && !currentTouchAlphabet.equals(mCurrentChosenAlphabet)) {
                    mCurrentChosenAlphabet = currentTouchAlphabet;
                    if (onLetterTouchedChangeListener != null) {
                        onLetterTouchedChangeListener.onTouchedLetterChange(mCurrentChosenAlphabet);
                    }
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                isTouched = false;
                break;
        }
        return true;
    }

    public interface OnLetterTouchedChangeListener {

        void onTouchedLetterChange(String letterTouched);

    }

}
